package com.kuaiditu.assistant.module.Interceptor;

import android.content.Context;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kuaiditu.assistant.App;
import com.kuaiditu.assistant.common.Constants;
import com.kuaiditu.assistant.module.storage.AssistantUserStorage;
import com.kuaiditu.assistant.ui.login.LoginActivity;

import javax.security.auth.login.LoginException;


/**
 * Created by Eenie on 2017/7/4.
 * Email: eeniework@gmail.com
 * Des:
 */


@Interceptor(priority = 5)
public class RouteLoginInterceptor implements IInterceptor {
    private Context mContext;
    private AssistantUserStorage mUserStorage;
    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {

        if (postcard.getExtra() == Constants.NEED_LOGIN_EXTRA) {
            if (mUserStorage.isLogin()) {
                callback.onContinue(postcard);
            } else {
                ARouter.getInstance().build(LoginActivity.PATH)
                        .greenChannel()
                        .navigation();
                callback.onInterrupt(new LoginException("need user,but no login"));
            }
        } else {
            callback.onContinue(postcard);
        }
}

    @Override
    public void init(Context context) {
        mContext = context;
        mUserStorage = ((App) context).getApplicationComponent().getUserStorage();
    }
}
